<?php
/**
 * @version $Id: issuu.php,v 2.16 $
 * @copyright (C) 2010 Yan Hong
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
 */
/** ensure this file is being included by a parent file */
//defined('_JEXEC') or die ('Direct Access to this location is not allowed.');

// jimport('joomla.plugin.plugin');

class plgContentIssuu
{
	function onContentPrepare($context, &$article, &$params, $limitstart)
	{
		// Entry point for Joomla 1.6
		$this->issuu_parser($article, $params, $limitstart);
	}

	function onPrepareContent(&$article, &$params, $limitstart)
	{
		// Entry point for Joomla 1.5
		$this->issuu_parser($article, $params, $limitstart);
	}

	function issuu_parser($text)
	{
		$regex = '/\[issuu (.*?)\]/i';
		preg_match_all($regex, $text, $matches);
		for ($x = 0; $x < count($matches[0]); $x++) {
			$replace = $this->issuu_switcher($matches[0][$x]);
			$text = str_replace($matches[0][$x], $replace, $text);
		}
		return $text;
	}

	function getValueWithDefault($regex, $params, $default)
	{
		$matchCount = preg_match_all($regex, $params, $matches);
		if ($matchCount) {
			return $matches[1][0];
		} else {
			return $default;
		}
	}

	function issuu_switcher($params)
	{
		$v = $this->getValueWithDefault('/(?:^|[\s]+)v=([\S]*)/i', $params, 1);
		switch ($v) {
			case 1:
				return $this->issuu_reader_1($params);
			case 2:
				return $this->issuu_reader_2($params);
			default:
				return $params;
		}
	}

	function issuu_reader_1($params)
	{
		$folderId = $this->getValueWithDefault('/(?:^|[\s]+)folderId=([\S]*)/i', $params, '');
		$documentId = $this->getValueWithDefault('/(?:^|[\s]+)documentId=([\S]*)/i', $params, '');
		$username = $this->getValueWithDefault('/(?:^|[\s]+)username=([\S]*)/i', $params, '');
		$docName = $this->getValueWithDefault('/(?:^|[\s]+)docName=([\S]*)/i', $params, '');
		$loadingInfoText = $this->getValueWithDefault('/(?:^|[\s]+)loadingInfoText=([\S]*)/i', $params, '');
		$tag = $this->getValueWithDefault('/(?:^|[\s]+)tag=([\S]*)/i', $params, '');
		$showFlipBtn = $this->getValueWithDefault('/(?:^|[\s]+)showFlipBtn=([\S]*)/i', $params, 'false');
		$proShowMenu = $this->getValueWithDefault('/(?:^|[\s]+)proShowMenu=([\S]*)/i', $params, 'false');
		$proShowSidebar = $this->getValueWithDefault('/(?:^|[\s]+)proShowSidebar=([\S]*)/i', $params, 'false');
		$autoFlip = $this->getValueWithDefault('/(?:^|[\s]+)autoFlip=([\S]*)/i', $params, 'false');
		$autoFlipTime = $this->getValueWithDefault('/(?:^|[\s]+)autoFlipTime=([\S]*)/i', $params, 0);
		$backgroundColor = $this->getValueWithDefault('/(?:^|[\s]+)backgroundColor=([\S]*)/i', $params, '');
		$layout = $this->getValueWithDefault('/(?:^|[\s]+)layout=([\S]*)/i', $params, '');
		$height = $this->getValueWithDefault('/(?:^|[\s]+)height=([\S]*)/i', $params, 301);
		$width = $this->getValueWithDefault('/(?:^|[\s]+)width=([\S]*)/i', $params, 450);
		$unit = 'px';//$this->getValueWithDefault('/(?:^|[\s]+)unit=([\S]*)/i', $params, 'px');
		$viewMode = $this->getValueWithDefault('/(?:^|[\s]+)viewMode=([\S]*)/i', $params, '');
		$pageNumber = $this->getValueWithDefault('/(?:^|[\s]+)pageNumber=([\S]*)/i', $params, 1);
		$logo = $this->getValueWithDefault('/(?:^|[\s]+)logo=([\S]*)/i', $params, '');
		$logoOffsetX = $this->getValueWithDefault('/(?:^|[\s]+)logoOffsetX=([\S]*)/i', $params, 0);
		$logoOffsetY = $this->getValueWithDefault('/(?:^|[\s]+)logoOffsetY=([\S]*)/i', $params, 0);
		$showHtmlLink = $this->getValueWithDefault('/(?:^|[\s]+)showHtmlLink=([\S]*)/i', $params, 'false');
		$valid = $this->getValueWithDefault('/(?:^|[\s]+)valid=([\S]*)/i', $params, 'false');

		$domain = 'issuu.com';

		$viewerUrl = 'http://static.' . $domain . '/webembed/viewers/style1/v1/IssuuViewer.swf';
		$standaloneUrl = 'http://' . $domain . '/' . $username . '/docs/' . $docName . '?mode=embed';
		$moreUrl = 'http://' . $domain . '/search?q=' . $tag;

		$flashVars = 'mode=embed';

		if ($folderId) {
			// load folder parameters
			$flashVars = $flashVars . '&amp;folderId=' . $folderId;
		} else {
			// load document parameters
			if ($documentId) {
				$flashVars = $flashVars . '&amp;documentId=' . $documentId;
			}
			if ($docName) {
				$flashVars = $flashVars . '&amp;docName=' . $docName;
			}
			if ($username) {
				$flashVars = $flashVars . '&amp;username=' . $username;
			}
			if ($loadingInfoText) {
				$flashVars = $flashVars . '&amp;loadingInfoText=' . $loadingInfoText;
			}
		}
		if ($showFlipBtn == 'true') {
			$flashVars = $flashVars . '&amp;showFlipBtn=' . $showFlipBtn;
		}
		if ($proShowMenu == 'true') {
			$flashVars = $flashVars . '&amp;proShowMenu=' . $proShowMenu;
		}
		if ($proShowSidebar == 'true') {
			$flashVars = $flashVars . '&amp;proShowSidebar=' . $proShowSidebar;
		}
		if ($autoFlip == 'true') {
			$flashVars = $flashVars . '&amp;autoFlip=' . $autoFlip;
			if ($autoFlipTime) {
				$flashVars = $flashVars . '&amp;autoFlipTime=' . $autoFlipTime;
			}
		}
		if ($backgroundColor) {
			$flashVars = $flashVars . '&amp;backgroundColor=' . $backgroundColor;
			$standaloneUrl = $standaloneUrl . '&amp;backgroundColor=' . $backgroundColor;
		}
		if ($layout) {
			$flashVars = $flashVars . '&amp;layout=' . $layout;
			$standaloneUrl = $standaloneUrl . '&amp;layout=' . $layout;
		}
		if ($viewMode) {
			$flashVars = $flashVars . '&amp;viewMode=' . $viewMode;
			$standaloneUrl = $standaloneUrl . '&amp;viewMode=' . $viewMode;
		}
		if ($pageNumber > 1) {
			$flashVars = $flashVars . '&amp;pageNumber=' . $pageNumber;
			$standaloneUrl = $standaloneUrl . '&amp;pageNumber=' . $pageNumber;
		}
		if ($logo) {
			$flashVars = $flashVars . '&amp;logo=' . $logo . '&amp;logoOffsetX=' . $logoOffsetX . '&amp;logoOffsetY=' . $logoOffsetY;
			$standaloneUrl = $standaloneUrl . '&amp;logo=' . $logo . '&amp;logoOffsetX=' . $logoOffsetX . '&amp;logoOffsetY=' . $logoOffsetY;
		}

		if ($valid == 'true') {
			// return w3c validity embed code
			return ( ($showHtmlLink == 'true') ? '<div>' : '') .
				'<object style="width:' . $width . $unit . ';height:' . $height . $unit. '" type="application/x-shockwave-flash" data="' . $viewerUrl . '?' . $flashVars . '">' .
				'<param name="movie" value="' . $viewerUrl . '?' . $flashVars . '"/>' .
				'<param name="allowfullscreen" value="true"/>' .
				'<param name="menu" value="false"/>' .
				'</object>';
		} else {
			// return default embed code
			return ( ($showHtmlLink == 'true') ? '<div>' : '') .
				'<object style="width:' . $width . $unit . ';height:' . $height . $unit. '; display: block; margin-left: auto; margin-right: auto;" >' .
				'<param name="movie" value="' . $viewerUrl . '?' . $flashVars . '" />' .
				'<param name="allowfullscreen" value="true"/>' .
				'<param name="menu" value="false"/>' .
				'<embed src="' . $viewerUrl . '" type="application/x-shockwave-flash" style="width:' . $width . $unit . ';height:' . $height . $unit . '" flashvars="' . $flashVars . '" allowfullscreen="true" menu="false" />' .
				'</object>';
		}
	}

	function issuu_reader_2($params)
	{
		$viewMode = $this->getValueWithDefault('/(?:^|[\s]+)viewMode=([\S]*)/i', $params, 'doublePage');
		$autoFlip = $this->getValueWithDefault('/(?:^|[\s]+)autoFlip=([\S]*)/i', $params, 'false');
		$width = $this->getValueWithDefault('/(?:^|[\s]+)width=([\S]*)/i', $params, 420);
		$height = $this->getValueWithDefault('/(?:^|[\s]+)height=([\S]*)/i', $params, 300);
		$unit = $this->getValueWithDefault('/(?:^|[\s]+)unit=([\S]*)/i', $params, 'px');
		$embedBackground = $this->getValueWithDefault('/(?:^|[\s]+)embedBackground=([\S]*)/i', $params, '');
		$pageNumber = $this->getValueWithDefault('/(?:^|[\s]+)pageNumber=([\S]*)/i', $params, 1);
		$titleBarEnabled = $this->getValueWithDefault('/(?:^|[\s]+)titleBarEnabled=([\S]*)/i', $params, 'false');
		$shareMenuEnabled = $this->getValueWithDefault('/(?:^|[\s]+)shareMenuEnabled=([\S]*)/i', $params, 'true');
		$showHtmlLink = $this->getValueWithDefault('/(?:^|[\s]+)showHtmlLink=([\S]*)/i', $params, 'true');
		$proSidebarEnabled = $this->getValueWithDefault('/(?:^|[\s]+)proSidebarEnabled=([\S]*)/i', $params, 'false');
		// Renamed proShowSidebar to proSidebarEnabled (Mar. 2011)
		if ($proSidebarEnabled == 'false') { // Backward compatible
			$proSidebarEnabled = $this->getValueWithDefault('/(?:^|[\s]+)proShowSidebar=([\S]*)/i', $params, 'false');
		}
		$printButtonEnabled = $this->getValueWithDefault('/(?:^|[\s]+)printButtonEnabled=([\S]*)/i', $params, 'true');
		$shareButtonEnabled = $this->getValueWithDefault('/(?:^|[\s]+)shareButtonEnabled=([\S]*)/i', $params, 'true');
		$searchButtonEnabled = $this->getValueWithDefault('/(?:^|[\s]+)searchButtonEnabled=([\S]*)/i', $params, 'true');
		$linkTarget = $this->getValueWithDefault('/(?:^|[\s]+)linkTarget=([\S]*)/i', $params, '_blank');
		$backgroundColor = $this->getValueWithDefault('/(?:^|[\s]+)backgroundColor=([\S]*)/i', $params, '');
		$theme = $this->getValueWithDefault('/(?:^|[\s]+)theme=([\S]*)/i', $params, 'default');
		$backgroundImage = $this->getValueWithDefault('/(?:^|[\s]+)backgroundImage=([\S]*)/i', $params, '');
		$backgroundStretch = $this->getValueWithDefault('/(?:^|[\s]+)backgroundStretch=([\S]*)/i', $params, 'false');
		$backgroundTile = $this->getValueWithDefault('/(?:^|[\s]+)backgroundTile=([\S]*)/i', $params, 'false');
		$layout = $this->getValueWithDefault('/(?:^|[\s]+)layout=([\S]*)/i', $params, '');
		$logo = $this->getValueWithDefault('/(?:^|[\s]+)logo=([\S]*)/i', $params, '');
		$documentId = $this->getValueWithDefault('/(?:^|[\s]+)documentId=([\S]*)/i', $params, '');
		$folderId = $this->getValueWithDefault('/(?:^|[\s]+)folderId=([\S]*)/i', $params, '');
		$name = $this->getValueWithDefault('/(?:^|[\s]+)name=([\S]*)/i', $params, '');
		$username = $this->getValueWithDefault('/(?:^|[\s]+)username=([\S]*)/i', $params, '');
		$tag = $this->getValueWithDefault('/(?:^|[\s]+)tag=([\S]*)/i', $params, '');
		$valid = $this->getValueWithDefault('/(?:^|[\s]+)valid=([\S]*)/i', $params, 'false');
		$scriptAccessEnabled = $this->getValueWithDefault('/(?:^|[\s]+)scriptAccessEnabled=([\S]*)/i', $params, 'false');
		$id = $this->getValueWithDefault('/(?:^|[\s]+)id=([\S]*)/i', $params, '');

		$domain = 'issuu.com';

		$readerUrl = 'http://static.' . $domain . '/webembed/viewers/style1/v2/IssuuReader.swf';
		$openUrl = 'http://' . $domain . '/' . $username . '/docs/' . $name . '?mode=window';
		$moreUrl = 'http://' . $domain . '/search?q=' . $tag;

//		$links = '<div style="width:' . $width . $unit . ';text-align:left;">' .
//			( $folderId ? '' : ('<a href="' . $openUrl . '" target="_blank">Open publication</a> - ') ) .
//			'Free <a href="http://' . $domain . '" target="_blank">publishing</a>' .
//			( $folderId ? '' : ( $tag ? (' - <a href="' . $moreUrl. '" target="_blank">More ' . urldecode($tag) . '</a>') : '' ) ) .
//			'</div>';

		$flashVars = 'mode=mini';
		// ****** embed options ******
		// layout
		if ($viewMode == 'doublePage') { // default value
		} else {
			$flashVars = $flashVars . '&amp;viewMode=' . $viewMode;
		}
		if ($autoFlip == 'false') { // default value
		} else {
			$flashVars = $flashVars . '&amp;autoFlip=' . $autoFlip;
		}
		// color
		if ($embedBackground) {
			$flashVars = $flashVars . '&amp;embedBackground=' . $embedBackground;
		}
		// start on
		if ($pageNumber == 1) { // default value
		} else {
			$flashVars = $flashVars . '&amp;pageNumber=' . $pageNumber;
		}
		// show
		if ($titleBarEnabled == 'false') { // default value
		} else {
			$flashVars = $flashVars . '&amp;titleBarEnabled=' . $titleBarEnabled;
		}
		if ($shareMenuEnabled == 'true') { // default value
		} else {
			$flashVars = $flashVars . '&amp;shareMenuEnabled=' . $shareMenuEnabled;
		}
		if ($proSidebarEnabled == 'false') { // default value
		} else {
			$flashVars = $flashVars . '&amp;proSidebarEnabled=' . $proSidebarEnabled;
		}
		// ****** fullscreen options ******
		// show
		if ($printButtonEnabled == 'true') { // default value
		} else {
			$flashVars = $flashVars . '&amp;printButtonEnabled=' . $printButtonEnabled;
		}
		if ($shareButtonEnabled == 'true') { // default value
		} else {
			$flashVars = $flashVars . '&amp;shareButtonEnabled=' . $shareButtonEnabled;
		}
		if ($searchButtonEnabled == 'true') { // default value
		} else {
			$flashVars = $flashVars . '&amp;searchButtonEnabled=' . $searchButtonEnabled;
		}
		// links
		if ($linkTarget == '_blank') { // default value
		} else {
			$flashVars = $flashVars . '&amp;linkTarget=' . $linkTarget;
		}
		// design
		if ($backgroundColor) {
			$flashVars = $flashVars . '&amp;backgroundColor=' . $backgroundColor;
		}
		if ($theme == 'default') { // default value
		} else {
			$flashVars = $flashVars . '&amp;theme=' . $theme;
		}
		if ($backgroundImage) {
			$flashVars = $flashVars . '&amp;backgroundImage=' . $backgroundImage;
		}
		if ($backgroundStretch == 'false') { // default value
		} else {
			$flashVars = $flashVars . '&amp;backgroundStretch=' . $backgroundStretch;
		}
		if ($backgroundTile == 'false') { // default value
		} else {
			$flashVars = $flashVars . '&amp;backgroundTile=' . $backgroundTile;
		}
		if ($layout) {
			$flashVars = $flashVars . '&amp;layout=' . $layout;
		}
		if ($logo) {
			$flashVars = $flashVars . '&amp;logo=' . $logo;
		}
		// ****** document information ******
		if ($documentId) {
			$flashVars = $flashVars . '&amp;documentId=' . $documentId;
		} else if ($folderId) {
			$flashVars = $flashVars . '&amp;folderId=' . $folderId;
		}

		if ($valid == 'true') {
			// return w3c validity embed code
			return ( ($showHtmlLink == 'true') ? '<div>' : '') .
				'<object style="width:' . $width . $unit . ';height:' . $height . $unit. '" type="application/x-shockwave-flash" data="' . $readerUrl . '?' . $flashVars . '">' .
				'<param name="movie" value="' . $readerUrl . '?' . $flashVars . '"/>' .
				'<param name="allowfullscreen" value="true"/>' .
				( ($linkTarget == '_blank' && $scriptAccessEnabled == 'false') ? '' : '<param name="allowscriptaccess" value="always"/>' ) .
				'<param name="menu" value="false"/>' .
				'<param name="wmode" value="transparent"/>' .
				'</object>' .
				( ($showHtmlLink == 'true') ? ( $links . '</div>' ) : '');
		} else {
			// return default embed code
			return ( ($showHtmlLink == 'true') ? '<div>' : '') .
				'<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ' .
				'style="width:' . $width . $unit . ';height:' . $height . $unit. '" ' .
				( ($id) ? ('id="' . $id . '" ') : '' ) . '>' .
				'<param name="movie" value="' . $readerUrl . '?' . $flashVars . '" />' .
				'<param name="allowfullscreen" value="true"/>' .
				( ($linkTarget == '_blank' && $scriptAccessEnabled == 'false') ? '' : '<param name="allowscriptaccess" value="always"/>' ) .
				'<param name="menu" value="false"/>' .
				'<param name="wmode" value="transparent"/>' .
				'<embed src="' . $readerUrl . '" ' .
				'type="application/x-shockwave-flash" ' .
				'style="width:' . $width . $unit . ';height:' . $height . $unit . '" ' .
				'flashvars="' . $flashVars . '" ' .
				'allowfullscreen="true" ' .
				( ($linkTarget == '_blank' && $scriptAccessEnabled == 'false') ? '' : 'allowscriptaccess="always" ' ) .
				'menu="false" ' .
				'wmode="transparent" />' .
				'</object>' .
				( ($showHtmlLink == 'true') ? ( $links . '</div>' ) : '');
		}
	}
}
?>
